<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>我是罗</title>
</head>
<body>
<div id="msg"></div>
<input type="text" id="text">
<input type="submit" value="发送数据" onclick="song()">

<!-- <input type="submit" value="开枪" onclick="kai()"> -->

</body>
<script>
    // https://segmentfault.com/q/1010000004942428

    var msg = document.getElementById("msg");
    var wsServer2 = 'ws://192.168.1.28:9501';//13770584200 	
    var base = 'ws://192.168.1.28:9501/?token=';
    var token = "abc123"
    var wsServer =base+token;

    //调用websocket对象建立连接：
    //参数：ws/wss(加密)：//ip:port （字符串）
    var websocket = new WebSocket(wsServer);
    //onopen监听连接打开
    websocket.onopen = function (evt) {
        //websocket.readyState 属性：
        /*
        CONNECTING    0    The connection is not yet open.
        OPEN    1    The connection is open and ready to communicate.
        CLOSING    2    The connection is in the process of closing.
        CLOSED    3    The connection is closed or couldn't be opened.
        */
        msg.innerHTML = websocket.readyState;
        //websocket.send("138");
    };

    function song(){
        var text = document.getElementById('text').value;
        document.getElementById('text').value = '';
        //向服务器发送数据
        //websocket.send(text);
        //text2 弄成jsonstr
        var message = "ho";
      
        var buzData = {key1:"key1",key2:"flagok"};
        var messageObj = {message:message,buzData:buzData};
        var messageJson = JSON.stringify(messageObj);
        

        websocket.send(messageJson);

    }


    function kai(){
        //碰撞本地判断，
        //我是A1
        //打到了石头，未击中
        //打到了小人 击中（击中了哪个小人。）
        // 转json 
        // target   {"目标"，"精灵B1"}
        // 精灵B1 唯一值 传给 服务器？
        websocket.send("我开了");
    }

    //jsclent
    function findEnemy(){
        //Onmessage 是 收到服务器的数据
        //服务的数据是其他 客户端组装的
        //服务器把敌人的3个坐标 收集了后发过来
        //我onmessage后 显示这3个敌人
    }

    //onMessage 只接收服务器信息
    //

      //监听连接关闭
//    websocket.onclose = function (evt) {
//        console.log("Disconnected");
//    };

    //onmessage 监听服务器数据推送
    websocket.onmessage = function (evt) {
        console.log("收到");
        console.log(evt.data);
        msg.innerHTML += evt.data +'<br>';
//        console.log('Retrieved data from server: ' + evt.data);
    };
//监听连接错误信息
//    websocket.onerror = function (evt, e) {
//        console.log('Error occured: ' + evt.data);
//    };

</script>
</html>



